package com.MartynovichIgor.KnowledgeTest.tools.admintools;

import com.MartynovichIgor.KnowledgeTest.tools.filetools.FileSaver;
import com.MartynovichIgor.KnowledgeTest.resources.ProjectPath;
import com.MartynovichIgor.KnowledgeTest.tools.filetools.FileLoader;
import com.MartynovichIgor.KnowledgeTest.tools.testtools.Parser;
import com.MartynovichIgor.KnowledgeTest.tools.usertools.UserSaver;
import java.io.*;
import java.util.ArrayList;

/**
 * Created by:
 * User: Igor Martynovich and Guzenko Igor
 * Date: 04.11.13
 */
public class Admin {
    private FileLoader fileLoader;
    private String usersFileName;
    private String statisticsFileName;
    private Parser parser;


    public Admin(){
        this.usersFileName = new ProjectPath().getProjectPath()+UserSaver.USER_INFO_FILE;
        this.statisticsFileName = new ProjectPath().getProjectPath()+UserSaver.USER_STATISTIC_FILE;
        this.fileLoader = new  FileLoader();
        this.parser = new Parser();
    }


    public void viewAllUsers() throws IOException {

        String usersInfo = fileLoader.readFile(usersFileName);
        System.out.printf("%s     %s\n\n","Login","Password");
        ArrayList<String> statistic = parser.getStringArray(usersInfo, "\n");
        for(String userInfoLine : statistic) {
            ArrayList<String> userFields = parser.getStringArray(userInfoLine, " ");
            System.out.printf("%s     %s\n", userFields.get(0), userFields.get(1));
        }
    }

    public void viewStatisticsByLogin(String userLogin) throws IOException {
           String allStatistics = fileLoader.readFile(statisticsFileName);
           ArrayList<String> statisticLines = parser.getStringArray(allStatistics,"\n");
           for(String line:statisticLines){
              if(parser.getStringArray(line,"/").get(0).equals(userLogin)){
                  printUserStatistics(line);
                  break;
              }
           }
    }

    private void printUserStatistics(String line) {
        ArrayList<String> statList = parser.getStringArray(line,"/");
        System.out.printf("\nThese statistics are for for the user with a login:%s", statList.get(0));
        for (int statNumber=1;statNumber<statList.size();statNumber++){
            System.out.printf("\n       %s", statList.get(statNumber));
        }
    }

    public void deleteUser(String userLogin) throws IOException {
        String loadedInfo = fileLoader.readFile(usersFileName);
        String loadedStat = fileLoader.readFile(statisticsFileName);
        FileSaver infoSaver = new FileSaver(usersFileName);
        FileSaver statisticsSaver = new FileSaver(statisticsFileName);
        ArrayList<String> usersInfoArray = parser.getStringArray(loadedInfo, "\n");
        ArrayList<String> usersStatisticsArray  = parser.getStringArray(loadedStat, "\n");
        for (int index = 0; index < usersInfoArray.size(); index++) {
            if (parser.getStringArray(usersInfoArray.get(index), " ").get(0).equals
                    (userLogin)){
                        usersInfoArray.remove(index);
                        usersStatisticsArray.remove(index);
                    }
        }
        infoSaver.rewriteAllFile(usersInfoArray);
        statisticsSaver.rewriteAllFile(usersStatisticsArray);
    }

    public void viewAllStatistics() throws IOException {
        String loadedStat = fileLoader.readFile(statisticsFileName);
        ArrayList<String> usersStatisticsArray  = parser.getStringArray(loadedStat, "\n");
        for (String userStats:usersStatisticsArray){
            printUserStatistics(userStats);
        }
    }
}
